package com.example.ytc.clazz;

import com.example.ytc.App;
import com.example.ytc.R;
import com.example.ytc.tools.FavoritDAO;

import android.content.Context;
import android.content.Intent;
import android.content.res.Resources;
import android.view.MenuItem;
import android.widget.Toast;

public class ActionButonEvent {
	Context mContext;
	public Video selected_video;
	public ActionButonEvent(Context mContext, Video selected_video) {
		super();
		this.mContext = mContext;
		this.selected_video = selected_video;
	}
	public boolean onOptionsItemSelected(MenuItem item) {

		int id = item.getItemId();
		switch (item.getItemId()) {
		case R.id.share:
			share();
			break;
		case R.id.addfavorite:
			add_to_favorits();

			break;
		case R.id.removefavorite:
			remove_favorits();

			break;
		default:

		}
		return true;

	}

	public void remove_favorits() {
		// TODO Auto-generated method stub
		String message;
		try {
			FavoritDAO.remove(this.mContext.getApplicationContext(), selected_video);
			message = String.format(
					this.mContext.getResources().getString(
							R.string.playlist_delete_this_playlist),
					selected_video.getTitel());
			App appState= ((App) mContext.getApplicationContext());
			appState.fav_list.notifyDataSetChanged();

		} catch (Exception e) {
			// TODO Auto-generated catch block
			message = String.format(
					this.mContext.getResources()
							.getString(R.string.playlist_could_not_delete),
					selected_video.getTitel());
			e.printStackTrace();
		}
		Toast.makeText(this.mContext, message, Toast.LENGTH_LONG).show();
		// initUI(selected_video);
	}

	public void add_to_favorits() {
		// TODO Auto-generated method stub
		String message;
		try {
			FavoritDAO.save(this.mContext.getApplicationContext(), selected_video);
			message = String.format(
					this.mContext.getResources().getString(
							R.string.playlist_successfully_added_to),
					selected_video.getTitel());

		} catch (Exception e) {
			// TODO Auto-generated catch block
			message = this.mContext.getResources().getString(
					R.string.playlist_could_not_be_added_to);
			e.printStackTrace();
		}
		Toast.makeText(this.mContext, message, Toast.LENGTH_LONG).show();
		// initUI(selected_video);
	}

	public void share() {
		Resources res = this.mContext.getResources();
		String text = String.format(res.getString(R.string.share_video_msg),
				selected_video.getTitel(), res.getString(R.string.app_name),
				"market://details?id="
						+ this.mContext.getApplicationContext().getPackageName());
		Intent myIntent = new Intent();
		myIntent.setAction(Intent.ACTION_SEND);
		myIntent.setType("text/plain");
		myIntent.putExtra(Intent.EXTRA_TEXT, text);
		myIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
		this.mContext.startActivity(myIntent);
	}

}
